﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WSManager.Model;
using WSManager.ModelValidation;
using FluentValidation;

namespace WSManager.Wpf.Dialogs
{
    /// <summary>
    /// Interaction logic for NewCategory.xaml
    /// </summary>
    partial class NewCategory: Window
    {
        Action<Category> _finished;
        readonly CategoryValidator _validator = new CategoryValidator();
        public NewCategory()
        {
            InitializeComponent();
        }

        public void Show(Action<Category> finished)
        {
            this._finished = finished;
            Show();
        }

        public void Show(IList<Category> listToAdd)
        {
            this._finished = listToAdd.Add;
            Show();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Validate().IsValid)
            {
                var category = CreateModel();
                _finished(category);
                Close();
            }
        }

        internal Category CreateModel()
        {
            return ModelRepairer.Repair(new Category()
            {
                Description = descriptionTb.Text,
                Name = nameTb.Text
            });
        }

        internal FluentValidation.Results.ValidationResult Validate()
        {
            var result = _validator.Validate(CreateModel());
            var nameFail = result.Errors.FirstOrDefault(r => r.PropertyName == "Name");
            var descFail = result.Errors.FirstOrDefault(r => r.PropertyName == "Description");
            if (nameFail != null)
            {
                nameTb.Style = FindResource("InvalidTextBox") as Style;
                nameTb.ToolTip = nameFail.ErrorMessage;
                nameValidation.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                nameTb.Style = null;
                nameValidation.Visibility = System.Windows.Visibility.Collapsed;
                nameTb.ToolTip = "";
            }
            if (descFail!= null)
            {
                descriptionTb.Style = FindResource("InvalidTextBox") as Style;
                descriptionTb.ToolTip = descFail.ErrorMessage;
            }
            else
            {
                descriptionTb.Style = null;
                descriptionTb.ToolTip = "";
            }
            return result;
        }
    }

    public class NewCategoryDialog
    {
        NewCategory _window;
        NewCategoryDialog()
        {

        }

        public static NewCategoryDialog Show(Action<Category> callback)
        {
            var d = new NewCategoryDialog();
            d._window = new NewCategory();
            d._window.Show(callback);
            return d;
        }
    }
}
